package com.corpgovernment.organization.service.impl;

import com.corpgovernment.organization.dto.MbFreshPassengerRelationDTO;
import com.corpgovernment.organization.entity.db.MbFreshPassengerRelation;
import com.corpgovernment.organization.mapper.MbFreshPassengerRelationMapper;
import com.ctrip.corp.obt.generic.utils.CollectionUtils;
import com.ctrip.corp.obt.generic.utils.StringUtils;
import com.ctrip.corp.obt.shard.context.PageContext;
import com.ctrip.corp.obt.shard.sql.mybatis.interceptor.pagination.IPage;
import com.ctrip.corp.obt.shard.sql.mybatis.interceptor.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 员工关联常旅
 */
@Service
@Slf4j
public class FreshPassengerRelationServiceImpl {


    @Autowired
    private MbFreshPassengerRelationMapper mbFreshPassengerRelationMapper;


    public MbFreshPassengerRelation selectByPk(Long id) {
        if (id == null) {
            return null;
        }
        return mbFreshPassengerRelationMapper.selectByPrimaryKey(id);
    }

    public List<MbFreshPassengerRelation> selectByUid(String uid) {

        if (StringUtils.isEmpty(uid)) {
            return new ArrayList<>();
        }

        MbFreshPassengerRelation selectParam = new MbFreshPassengerRelation();
        selectParam.setIsDeleted(false);
        selectParam.setUid(uid);
        return mbFreshPassengerRelationMapper.select(selectParam);
    }

    public Page<MbFreshPassengerRelation> pageByUid(String uid, String orgId, String query, Integer pageNum, Integer pageSize) {
        List<MbFreshPassengerRelationDTO> freshPassengerRelationList = mbFreshPassengerRelationMapper.pageByUid(uid);
        //模糊查询
        if (StringUtils.isNotBlank(query)){
            freshPassengerRelationList = freshPassengerRelationList.stream().filter(e -> StringUtils.contains(e.getName(), query)|| StringUtils.contains(e.getEnName(), query)).collect(Collectors.toList());
        }

        List<Long> ids = freshPassengerRelationList.stream().filter(item -> StringUtils.isBlank(item.getOrgId())).map(MbFreshPassengerRelationDTO::getId).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(ids)){
            ids = new ArrayList<>();
        }
        Optional<MbFreshPassengerRelationDTO> first = freshPassengerRelationList.stream().filter(item -> StringUtils.isNotBlank(item.getOrgId())).findFirst();
        if (first.isPresent()){
            ids.add(first.get().getId());
        }
        if (CollectionUtils.isEmpty(ids)) {
            return new Page(pageNum,pageSize);
        }
        IPage<MbFreshPassengerRelation> iPage = PageContext.startPage(pageNum, pageSize);
        mbFreshPassengerRelationMapper.selectInIds(ids);
        Page<MbFreshPassengerRelation> page = new Page<>(iPage.getCurrent(),iPage.getSize(),iPage.getTotal());
        page.setRecords(iPage.getRecords());
        return page;

    }

    public void save(List<MbFreshPassengerRelation> freshPassengerRelationList) {
        mbFreshPassengerRelationMapper.insertList(freshPassengerRelationList);
    }

    public boolean save(MbFreshPassengerRelation mbFreshPassengerRelation) {
        if (mbFreshPassengerRelation == null) {
            return false;
        }
        return mbFreshPassengerRelationMapper.insertSelective(mbFreshPassengerRelation) > 0;
    }

    public void updateFreshTime(List<Long> freshTimeList) {
        mbFreshPassengerRelationMapper.updateFreshTime(freshTimeList);
    }

    public void deletedRelation(List<Long> deletedList) {
        mbFreshPassengerRelationMapper.deletedRelations(deletedList);
    }

    public boolean deletedRelation(String uid, String freshUid) {
        return mbFreshPassengerRelationMapper.deletedRelation(uid, freshUid) > 0;
    }

    public boolean deletedRelationByUid(String uid){
        return mbFreshPassengerRelationMapper.deletedRelationByUid(uid) > 0;
    }

}
